package com.smart4j.chapter.modules.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.smart4j.chapter.modules.entity.Client;
import com.smart4j.chapter.modules.service.ClientService;
import com.smart4j.chapter.modules.service.impl.ClientServiceImpl;

/**
 * 保存客户 添加 或 修改的数据
 */
@WebServlet("/clientSaveServlet")
public class ClientSaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private ClientService<Client> clientService = new ClientServiceImpl();

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ClientSaveServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		if(StringUtils.isBlank(id)){
			clientService.add(new Client(),request);
		}else{
			clientService.update(new Client(),request);
		}
		
		response.sendRedirect(request.getContextPath()+"/clientListServlet");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
